﻿using System.Collections.Generic;
using System.Threading.Tasks;
using WeiboWPSdk.Json;
using WeiboWPSdk.ResponseEntity;

namespace WeiboWPSdk.Api {
    public class FriendshipApi : ApiBase {
        public FriendshipApi(ClientHttpHelper httpHelper)
            : base(httpHelper) {
        }

        #region 关系读取接口
        /// <summary>
        /// 获取用户的关注列表
        /// http://open.weibo.com/wiki/2/friendships/friends
        /// 参数uid与screen_name二者必选其一，且只能选其一
        /// </summary>
        /// <param name="uid">需要查询的用户UID。</param>
        /// <param name="screenName">需要查询的用户昵称。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最大不超过200。</param>
        /// <param name="cursor">返回结果的游标，下一页用返回值里的next_cursor，上一页用previous_cursor，默认为0。</param>
        /// <param name="trimStatus">返回值中user字段中的status字段开关，0：返回完整status字段、1：status字段仅返回status_id，默认为1。</param>
        /// <returns></returns>
        public async Task<UserCollection> GetFriendsAsync(long? uid = null, string screenName = null, int count = 50, int cursor = 0, bool trimStatus = true) {
            var json = await HttpHelper.GetBasicCommandAsync("friendships/friends", new WeiboParameter("uid", uid), new WeiboParameter("screen_name", screenName),
                new WeiboParameter("count", count), new WeiboParameter("cursor", cursor), new WeiboParameter("trim_status", trimStatus));
            return JsonConvert.DeserializeObject<UserCollection>(json);
        }
        /// <summary>
        /// 获取两个用户之间的共同关注人列表
        /// http://open.weibo.com/wiki/2/friendships/friends/in_common
        /// </summary>
        /// <param name="uid">需要获取共同关注关系的用户UID。</param>
        /// <param name="suid">需要获取共同关注关系的用户UID，默认为当前登录用户。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="trimStatus">返回值中user字段中的status字段开关，0：返回完整status字段、1：status字段仅返回status_id，默认为1。</param>
        /// <returns></returns>
        public async Task<UserCollection> GetFriendsInCommonAsync(long uid, long? suid = null, int count = 50, int page = 1, bool trimStatus = true) {
            var json = await HttpHelper.GetBasicCommandAsync("friendships/friends/in_common", new WeiboParameter("uid", uid), new WeiboParameter("suid", suid), new WeiboParameter("count", count),
                new WeiboParameter("page", page), new WeiboParameter("trim_status", trimStatus));
            return JsonConvert.DeserializeObject<UserCollection>(json);
        }
        /// <summary>
        /// 获取用户的双向关注列表，即互粉列表
        /// http://open.weibo.com/wiki/2/friendships/friends/bilateral
        /// 排序功能目前只支持一种
        /// </summary>
        /// <param name="uid">需要获取双向关注列表的用户UID。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="sort">排序类型，0：按关注时间最近排序，默认为0。</param>
        /// <returns></returns>
        public async Task<UserCollection> GetBilateralFriendsAsync(long uid, int count = 50, int page = 1, int sort = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("friendships/friends/bilateral", new WeiboParameter("uid", uid), new WeiboParameter("count", count),
                new WeiboParameter("page", page), new WeiboParameter("sort", sort));
            return JsonConvert.DeserializeObject<UserCollection>(json);
        }
        /// <summary>
        /// 获取用户双向关注的用户ID列表，即互粉UID列表
        /// http://open.weibo.com/wiki/2/friendships/friends/bilateral/ids
        /// 排序功能目前只支持一种
        /// </summary>
        /// <param name="uid">需要获取双向关注列表的用户UID。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="sort">排序类型，0：按关注时间最近排序，默认为0。</param>
        /// <returns></returns>
        public async Task<UserCollection> GetBilateralFriendsIdsAsync(long uid, int count = 50, int page = 1, int sort = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("friendships/friends/bilateral/ids", new WeiboParameter("uid", uid), new WeiboParameter("count", count),
                new WeiboParameter("page", page), new WeiboParameter("sort", sort));
            return JsonConvert.DeserializeObject<UserCollection>(json);
        }
        /// <summary>
        /// 获取两个用户之间的详细关注关系情况
        /// http://open.weibo.com/wiki/2/friendships/show
        /// 参数source_id与source_screen_name二者必选其一，且只能选其一
        /// 参数target_id与target_screen_name二者必选其一，且只能选其一
        /// </summary>
        /// <param name="sourceId">源用户的UID。</param>
        /// <param name="sourceScreenName">源用户的微博昵称。</param>
        /// <param name="targetId">目标用户的UID。</param>
        /// <param name="targetScreenName">目标用户的微博昵称。</param>
        /// <returns></returns>
        public async Task<Friendship> ShowAsync(long? sourceId = null, string sourceScreenName = null, long? targetId = null, string targetScreenName = null) {
            var json = await HttpHelper.GetBasicCommandAsync("friendships/show", new WeiboParameter("source_id", sourceId), new WeiboParameter("source_screen_name", sourceScreenName),
                new WeiboParameter("target_id", targetId), new WeiboParameter("target_screen_name", targetScreenName));
            return JsonConvert.DeserializeObject<Friendship>(json);
        }
        #endregion
        #region 粉丝读取接口
        /// <summary>
        /// 获取用户的粉丝列表
        /// http://open.weibo.com/wiki/2/friendships/followers
        /// 参数uid与screen_name二者必选其一，且只能选其一；最多返回5000条数据
        /// </summary>
        /// <param name="uid">需要查询的用户UID。</param>
        /// <param name="screenName">需要查询的用户昵称。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最大不超过200。</param>
        /// <param name="cursor">返回结果的游标，下一页用返回值里的next_cursor，上一页用previous_cursor，默认为0。</param>
        /// <param name="trimStatus">返回值中user字段中的status字段开关，0：返回完整status字段、1：status字段仅返回status_id，默认为1。</param>
        /// <returns></returns>
        public async Task<UserCollection> GetFollowersAsync(long? uid = null, string screenName = null, int count = 50, int cursor = 0, bool trimStatus = true) {
            var json = await HttpHelper.GetBasicCommandAsync("friendships/followers", new WeiboParameter("uid", uid), new WeiboParameter("screen_name", screenName),
                new WeiboParameter("count", count), new WeiboParameter("cursor", cursor), new WeiboParameter("trim_status", trimStatus));
            return JsonConvert.DeserializeObject<UserCollection>(json);
        }
        /// <summary>
        /// 获取用户粉丝的用户UID列表
        /// http://open.weibo.com/wiki/2/friendships/followers/ids
        /// 参数uid与screen_name二者必选其一，且只能选其一
        /// </summary>
        /// <param name="uid">需要查询的用户UID。</param>
        /// <param name="screenName">需要查询的用户昵称。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最大不超过200。</param>
        /// <param name="cursor">返回结果的游标，下一页用返回值里的next_cursor，上一页用previous_cursor，默认为0。</param>
        /// <returns></returns>
        public async Task<UserCollection> GetFollowersIdsAsync(long? uid = null, string screenName = null, int count = 50, int cursor = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("friendships/followers/ids", new WeiboParameter("uid", uid), new WeiboParameter("screen_name", screenName),
                new WeiboParameter("count", count), new WeiboParameter("cursor", cursor));
            return JsonConvert.DeserializeObject<UserCollection>(json);
        }
        /// <summary>
        /// 获取用户的活跃粉丝列表
        /// http://open.weibo.com/wiki/2/friendships/followers/active
        /// </summary>
        /// <param name="uid">需要查询的用户UID。</param>
        /// <param name="count">返回的记录条数，默认为20，最大不超过200。</param>
        /// <returns></returns>
        public async Task<IEnumerable<User>> GetActiveFollowersAsync(long uid, int count = 20) {
            var json = await HttpHelper.GetBasicCommandAsync("friendships/followers/active", new WeiboParameter("uid", uid), new WeiboParameter("count", count));
            return JsonConvert.DeserializeObject<IEnumerable<User>>(json);
        }
        #endregion
        #region 关系链读取接口
        /// <summary>
        /// 获取当前登录用户的关注人中又关注了指定用户的用户列表
        /// http://open.weibo.com/wiki/2/friendships/friends_chain/followers
        /// </summary>
        /// <param name="uid">指定的关注目标用户UID。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<UserCollection> GetFriendsChainFollowersAsync(long uid, int count = 50, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("friendships/friends_chain/followers", new WeiboParameter("uid", uid), new WeiboParameter("count", count),
                new WeiboParameter("page", page));
            return JsonConvert.DeserializeObject<UserCollection>(json);
        }
        #endregion
        #region 写入接口
        /// <summary>
        /// 关注一个用户
        /// http://open.weibo.com/wiki/2/friendships/create
        /// </summary>
        /// <param name="uid">需要关注的用户ID。</param>
        /// <param name="screenName">需要关注的用户昵称。</param>
        /// <returns></returns>
        public async Task<User> CreateAsync(long? uid = null, string screenName = null) {
            var json = await HttpHelper.PostBasicCommandAsync("friendships/create", new WeiboParameter("uid", uid), new WeiboParameter("screenName", screenName));
            return JsonConvert.DeserializeObject<User>(json);
        }
        /// <summary>
        /// 取消关注一个用户
        /// http://open.weibo.com/wiki/2/friendships/destroy
        /// </summary>
        /// <param name="uid">需要取消关注的用户ID。</param>
        /// <param name="screenName">需要取消关注的用户昵称。</param>
        /// <returns></returns>
        public async Task<User> DestroyAsync(long? uid = null, string screenName = null) {
            var json = await HttpHelper.PostBasicCommandAsync("friendships/destroy", new WeiboParameter("uid", uid), new WeiboParameter("screenName", screenName));
            return JsonConvert.DeserializeObject<User>(json);
        }
        #endregion
    }
}
